// 系统中所有固定类型
// 获取最新的类型 dispatch('types/query', type)
// 使用方式：
// computed: {
//     workProject() {
//         return this.$store.getters.types.workProject || []
//     }
// }
// created () {
//     this.getTypes()
// }
// methods: {
//     getTypes() {
//         this.$store.dispatch('types/query', ['workProject'])
//     }
// }
const defaultState = () => {
    return {
        // 检查结果
        checkResults: [],
        // 项目
        workProject: [],
        // 巡查类型
        typeInspection: [],
        // 缺陷类型
        typeDefect: [],
        // 天气
        weather: [],
        // 项目缺陷列表
        projectDefectType: [],
        // 涵洞类型
        culvertType: [],
        // 涵洞检查内容
        culvertCheckContentOptions: [],
        // 桥梁检查内容
        bridgeCheckContentOptions: [],
        // 路基边坡检查内容
        roadbedCheckContentOptions: [],
        // 隧道土建检查内容
        tunnelCheckContentOptions1: [],
        // 隧道机电检查内容
        tunnelCheckContentOptions2: [],
        // 养护任务单状态
        maintenanceTaskStatus: [],
        // 日常养护计量申请类型
        maintenanceApplyTypes: []
    }
}

const getters = {}

const mutations = {
    setTypes: (state, { type, data }) => {
        state[type] = data
    }
}

// 静态字典
const staticTypes = {
    checkResults: [
        {
            id: 1,
            name: '未检查'
        },
        {
            id: 2,
            name: '检查完成'
        }
    ],
    // 养护任务单状态
    maintenanceTaskStatus: [
        {
            id: 0,
            name: '未提交',
            class: 'text-orange-light'
        },
        {
            id: 1,
            name: '审批中',
            class: 'text-orange-light'
        },
        {
            id: 2,
            name: '审批驳回',
            class: 'text-red-dark'
        },
        {
            id: 3,
            name: '已撤回',
            class: 'text-gray-light'
        },
        {
            id: 4,
            name: '审批通过',
            class: 'text-green-light'
        },
        {
            id: 5,
            name: '超时未处理',
            class: 'text-red-dark'
        },
        {
            id: 6,
            name: '处理中',
            class: 'text-orange-light'
        },
        {
            id: 7,
            name: '已拒绝',
            class: 'text-red-dark'
        },
        {
            id: 8,
            name: '待核实',
            class: 'text-gray-light'
        },
        {
            id: 9,
            name: '已完成',
            class: 'text-gray-light'
        },
        {
            id: 10,
            name: '超时完成',
            class: 'text-gray-light'
        }
    ],
    // 日常养护计量申请类型
    maintenanceApplyTypes: [
        {
            id: 1,
            name: '日常养护'
        },
        {
            id: 2,
            name: '破损被盗'
        },
        {
            id: 3,
            name: '抗冰保通'
        },
        {
            id: 4,
            name: '汛期值守'
        },
    ]

}

// fetchLocal 表示数据从本地静态字典中读取
const fetchLocal = 'local'

// 记录字典对后端类型 code 的映射关系
const mapper = {
    checkResults: fetchLocal,
    // 项目
    workProject: 'workProject',
    // 天气类型
    weather: 'weatherType',
    // 巡查类型
    typeInspection: 'inspectionType',
    // 缺陷类型
    typeDefect: 'typeDefect',
    // 项目缺陷类别， 后面变动，该字典就用workProject
    projectDefectType: 'defectType',
    // 涵洞类型
    culvertType: 'culvertType',
    // 涵洞检查内容
    culvertCheckContentOptions: 'culvertCheckContent',
    // 桥梁检查内容
    bridgeCheckContentOptions: 'bridgeCheckContent',
    // 路基边坡检查内容
    roadbedCheckContentOptions: 'roadbedCheckContent',
    // 隧道土建检查内容
    tunnelCheckContentOptions1: 'tunnelEmecOther',
    // 隧道机电检查内容
    tunnelCheckContentOptions2: 'tunnelEmec',
    maintenanceTaskStatus: fetchLocal,
    // 日常养护计量申请类型
    maintenanceApplyTypes: fetchLocal
}

import { DictionaryModel } from '@/models'
const model = new DictionaryModel()

const loadTypes = type => {
    return new Promise((resolve, reject) => {
        const code = mapper[type]
        if (code === undefined) {
            console.log(`type value is not found in mappers, type: ${type}`)
        }

        if (code === fetchLocal) {
            const data = staticTypes[type]
            resolve(data)
        } else {
            model
                .getTypes(code)
                .then(data => {
                    resolve(data)
                })
                .catch(err => {
                    reject(err)
                })
        }
    })
}

const actions = {
    query({ commit, state }, type) {
        const types = [].concat(type)
        types.forEach(type => {
            loadTypes(type)
                .then(data => {
                    commit('setTypes', { type, data })
                })
                .catch(err => console.error('load type error', err))
        })
    }
}

export default {
    namespaced: true,
    state: defaultState(),
    getters,
    mutations,
    actions
}
